/*     */ package com.zimbra.qa.unittest.prov.ldap;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.qa.unittest.TestUtil;
/*     */ import com.zimbra.qa.unittest.prov.BinaryLdapData.Content;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.junit.AfterClass;
/*     */ import org.junit.Assert;
/*     */ import org.junit.BeforeClass;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestLdapBinary
/*     */   extends LdapTest
/*     */ {
/*     */   private static LdapProvTestUtil provUtil;
/*     */   private static Provisioning prov;
/*     */   private static Domain domain;
/*     */   private static final String USER = "test-ldap-binary";
/*     */   private static final String CONTENT_NAME = "/opt/zimbra/unittest/ldap/binaryContent/zimbra.jpeg";
/*     */   private static final String CONTENT_NAME_1 = "/opt/zimbra/unittest/ldap/binaryContent/1.jpeg";
/*     */   private static final String CONTENT_NAME_2 = "/opt/zimbra/unittest/ldap/binaryContent/2.jpeg";
/*     */   private static final String CONTENT_NAME_3 = "/opt/zimbra/unittest/ldap/binaryContent/3.jpeg";
/*     */   private static final String CERT_NAME_1 = "/opt/zimbra/unittest/ldap/binaryContent/user1_primary.DER.crt";
/*     */   private static final String CERT_NAME_2 = "/opt/zimbra/unittest/ldap/binaryContent/user1_alias.DER.crt";
/*     */   private static final String CONTENT_NAME_TOO_LARGE = "/opt/zimbra/unittest/ldap/binaryContent/too_large.txt";
/*  77 */   private static SingleValuedTestData ZIMBRA_BINARY_SINGLE = new SingleValuedTestData("zimbraUnittestBinary", "/opt/zimbra/unittest/ldap/binaryContent/zimbra.jpeg");
/*     */   
/*     */ 
/*  80 */   private static SingleValuedTestData ZIMBRA_CERTIFICATE_SINGLE = new SingleValuedTestData("zimbraUnittestCertificate", "/opt/zimbra/unittest/ldap/binaryContent/user1_primary.DER.crt");
/*     */   
/*     */ 
/*  83 */   private static SingleValuedTestData ZIMBRA_BINARY_SINGLE_TEST_TOO_LARGE = new SingleValuedTestData("zimbraUnittestBinary", "/opt/zimbra/unittest/ldap/binaryContent/too_large.txt");
/*     */   
/*     */ 
/*  86 */   private static MultiValuedTestData ZIMBRA_BINARY_MULTI = new MultiValuedTestData("zimbraPrefMailSMIMECertificate", new String[] { "/opt/zimbra/unittest/ldap/binaryContent/1.jpeg", "/opt/zimbra/unittest/ldap/binaryContent/2.jpeg", "/opt/zimbra/unittest/ldap/binaryContent/3.jpeg" }, new String[] { "/opt/zimbra/unittest/ldap/binaryContent/1.jpeg", "/opt/zimbra/unittest/ldap/binaryContent/2.jpeg" }, new String[] { "/opt/zimbra/unittest/ldap/binaryContent/3.jpeg" });
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  93 */   private static MultiValuedTestData CERTIFICATE_MULTI = new MultiValuedTestData("userCertificate", new String[] { "/opt/zimbra/unittest/ldap/binaryContent/user1_primary.DER.crt", "/opt/zimbra/unittest/ldap/binaryContent/user1_alias.DER.crt" }, new String[] { "/opt/zimbra/unittest/ldap/binaryContent/user1_primary.DER.crt" }, new String[] { "/opt/zimbra/unittest/ldap/binaryContent/user1_alias.DER.crt" });
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 100 */   private static MultiValuedTestData RFC2252_BINARY_MULTI = new MultiValuedTestData("userSMIMECertificate", new String[] { "/opt/zimbra/unittest/ldap/binaryContent/1.jpeg", "/opt/zimbra/unittest/ldap/binaryContent/2.jpeg", "/opt/zimbra/unittest/ldap/binaryContent/3.jpeg" }, new String[] { "/opt/zimbra/unittest/ldap/binaryContent/1.jpeg", "/opt/zimbra/unittest/ldap/binaryContent/2.jpeg" }, new String[] { "/opt/zimbra/unittest/ldap/binaryContent/3.jpeg" });
/*     */   
/*     */ 
/*     */ 
/*     */   private static class SingleValuedTestData
/*     */   {
/*     */     String attrName;
/*     */     
/*     */     String content;
/*     */     
/*     */ 
/*     */     SingleValuedTestData(String attrName, String content)
/*     */     {
/* 113 */       this.attrName = attrName;
/* 114 */       this.content = content;
/*     */     }
/*     */   }
/*     */   
/*     */   private static class MultiValuedTestData
/*     */   {
/*     */     String attrName;
/*     */     String[] contents;
/*     */     String[] contentsToRemove;
/*     */     String[] contentsRemaining;
/*     */     
/*     */     MultiValuedTestData(String attrName, String[] contents, String[] contentsToRemove, String[] contentsRemaining) {
/* 126 */       this.attrName = attrName;
/* 127 */       this.contents = contents;
/* 128 */       this.contentsToRemove = contentsToRemove;
/* 129 */       this.contentsRemaining = contentsRemaining;
/*     */     }
/*     */   }
/*     */   
/*     */   @BeforeClass
/*     */   public static void init() throws Exception
/*     */   {
/* 136 */     provUtil = new LdapProvTestUtil();
/* 137 */     prov = provUtil.getProv();
/* 138 */     domain = provUtil.createDomain(baseDomainName(), null);
/*     */     
/* 140 */     Account acct = provUtil.createAccount("test-ldap-binary", domain);
/*     */   }
/*     */   
/*     */   @AfterClass
/*     */   public static void cleanup() throws Exception {
/* 145 */     Cleanup.deleteAll(new String[] { baseDomainName() });
/*     */   }
/*     */   
/*     */   private Entry getEntry() throws Exception {
/* 149 */     String entryName = TestUtil.getAddress("test-ldap-binary", domain.getName());
/* 150 */     return prov.get(Key.AccountBy.name, entryName);
/*     */   }
/*     */   
/*     */   private void delete(String attrName) throws Exception {
/* 154 */     Entry entry = getEntry();
/*     */     
/* 156 */     Map<String, Object> attrs = new HashMap();
/* 157 */     attrs.put(attrName, null);
/* 158 */     prov.modifyAttrs(entry, attrs);
/*     */     
/* 160 */     byte[] value = entry.getBinaryAttr(attrName);
/* 161 */     Assert.assertNull(value);
/*     */   }
/*     */   
/*     */   private void modify(String attrName, String contentName) throws Exception
/*     */   {
/* 166 */     Entry entry = getEntry();
/*     */     
/* 168 */     Map<String, Object> attrs = new HashMap();
/* 169 */     attrs.put(attrName, BinaryLdapData.Content.getContentByFileName(contentName).getString());
/* 170 */     prov.modifyAttrs(entry, attrs);
/*     */   }
/*     */   
/*     */   private void modify(String attrName, String[] contentNames) throws Exception
/*     */   {
/* 175 */     Entry entry = getEntry();
/*     */     
/* 177 */     Map<String, Object> attrs = new HashMap();
/* 178 */     for (String contentName : contentNames) {
/* 179 */       StringUtil.addToMultiMap(attrs, attrName, BinaryLdapData.Content.getContentByFileName(contentName).getString());
/*     */     }
/* 181 */     prov.modifyAttrs(entry, attrs);
/*     */   }
/*     */   
/*     */   private void replaceAttr(String attrName, String contentName) throws Exception
/*     */   {
/* 186 */     modify(attrName, contentName);
/*     */   }
/*     */   
/*     */   private void addAttr(String attrName, String contentName) throws Exception {
/* 190 */     modify("+" + attrName, contentName);
/*     */   }
/*     */   
/*     */   private void removeAttr(String attrName, String contentName) throws Exception {
/* 194 */     modify("-" + attrName, contentName);
/*     */   }
/*     */   
/*     */   private void replaceAttr(String attrName, String[] contentNames) throws Exception {
/* 198 */     modify(attrName, contentNames);
/*     */   }
/*     */   
/*     */   private void addAttr(String attrName, String[] contentNames) throws Exception {
/* 202 */     modify("+" + attrName, contentNames);
/*     */   }
/*     */   
/*     */   private void removeAttr(String attrName, String[] contentNames) throws Exception {
/* 206 */     modify("-" + attrName, contentNames);
/*     */   }
/*     */   
/*     */   private void verify(String attrName, String contentName) throws Exception {
/* 210 */     Entry entry = getEntry();
/* 211 */     String stringValue = entry.getAttr(attrName);
/* 212 */     byte[] binaryValue = entry.getBinaryAttr(attrName);
/*     */     
/* 214 */     BinaryLdapData.Content content = BinaryLdapData.Content.getContentByFileName(contentName);
/* 215 */     Assert.assertTrue(content.equals(stringValue));
/* 216 */     Assert.assertTrue(content.equals(binaryValue));
/*     */   }
/*     */   
/*     */   private void verify(String attrName, String[] contentNames) throws Exception {
/* 220 */     int numContents = contentNames.length;
/*     */     
/* 222 */     Entry entry = getEntry();
/* 223 */     String[] stringValues = entry.getMultiAttr(attrName);
/* 224 */     List<byte[]> binaryValues = entry.getMultiBinaryAttr(attrName);
/*     */     
/* 226 */     Assert.assertEquals(numContents, stringValues.length);
/* 227 */     Assert.assertEquals(numContents, binaryValues.size());
/*     */     
/* 229 */     for (int i = 0; i < numContents; i++) {
/* 230 */       BinaryLdapData.Content content = BinaryLdapData.Content.getContentByFileName(contentNames[i]);
/*     */       
/* 232 */       boolean found = false;
/* 233 */       for (int j = 0; (j < stringValues.length) && (!found); j++) {
/* 234 */         if (content.equals(stringValues[j])) {
/* 235 */           found = true;
/*     */         }
/*     */       }
/* 238 */       Assert.assertTrue(found);
/*     */       
/* 240 */       found = false;
/* 241 */       for (int j = 0; (j < binaryValues.size()) && (!found); j++) {
/* 242 */         if (content.equals((byte[])binaryValues.get(j))) {
/* 243 */           found = true;
/*     */         }
/*     */       }
/* 246 */       Assert.assertTrue(found);
/*     */     }
/*     */   }
/*     */   
/*     */   private void verifyIsEmpty(Entry entry, String attrName) {
/* 251 */     String value = entry.getAttr(attrName);
/* 252 */     Assert.assertNull(value);
/*     */   }
/*     */   
/*     */   private void replaceSingle(SingleValuedTestData data) throws Exception {
/* 256 */     String attrName = data.attrName;
/* 257 */     String contentName = data.content;
/*     */     
/* 259 */     delete(attrName);
/*     */     
/* 261 */     replaceAttr(attrName, contentName);
/* 262 */     verify(attrName, contentName);
/*     */   }
/*     */   
/*     */   private void replaceMulti(MultiValuedTestData data) throws Exception {
/* 266 */     String attrName = data.attrName;
/* 267 */     String[] contentNames = data.contents;
/*     */     
/* 269 */     delete(attrName);
/*     */     
/* 271 */     replaceAttr(attrName, contentNames);
/* 272 */     verify(attrName, contentNames);
/*     */   }
/*     */   
/*     */   private void remove(MultiValuedTestData data, boolean array) throws Exception {
/* 276 */     String attrName = data.attrName;
/* 277 */     String[] contentNames = data.contents;
/*     */     
/* 279 */     delete(attrName);
/*     */     
/* 281 */     replaceAttr(attrName, contentNames);
/* 282 */     verify(attrName, contentNames);
/*     */     
/* 284 */     String[] contentNamesToRemove = data.contentsToRemove;
/* 285 */     String[] contentNamesRemaining = data.contentsRemaining;
/*     */     
/* 287 */     if (array) {
/* 288 */       removeAttr(attrName, contentNamesToRemove);
/*     */     } else {
/* 290 */       for (String contentName : contentNamesToRemove) {
/* 291 */         removeAttr(attrName, contentName);
/*     */       }
/*     */     }
/*     */     
/* 295 */     verify(attrName, contentNamesRemaining);
/*     */   }
/*     */   
/*     */   private void add(MultiValuedTestData data, boolean array) throws Exception {
/* 299 */     String attrName = data.attrName;
/* 300 */     String[] contentNames = data.contents;
/*     */     
/* 302 */     delete(attrName);
/*     */     
/* 304 */     if (array) {
/* 305 */       addAttr(attrName, contentNames);
/*     */     } else {
/* 307 */       for (String contentName : contentNames) {
/* 308 */         addAttr(attrName, contentName);
/*     */       }
/*     */     }
/*     */     
/* 312 */     verify(attrName, contentNames);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testTooLarge() throws Exception {
/* 317 */     SingleValuedTestData data = ZIMBRA_BINARY_SINGLE_TEST_TOO_LARGE;
/*     */     
/* 319 */     String attrName = data.attrName;
/* 320 */     String contentName = data.content;
/*     */     
/* 322 */     delete(attrName);
/*     */     
/* 324 */     boolean caught = false;
/*     */     try {
/* 326 */       replaceAttr(attrName, contentName);
/*     */     } catch (ServiceException e) {
/* 328 */       if ("account.INVALID_ATTR_VALUE".equals(e.getCode())) {
/* 329 */         caught = true;
/*     */       }
/*     */     }
/*     */     
/* 333 */     Assert.assertTrue(caught);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testReplaceSingle() throws Exception {
/* 338 */     replaceSingle(ZIMBRA_BINARY_SINGLE);
/* 339 */     replaceSingle(ZIMBRA_CERTIFICATE_SINGLE);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testReplaceMulti() throws Exception {
/* 344 */     replaceMulti(ZIMBRA_BINARY_MULTI);
/* 345 */     replaceMulti(CERTIFICATE_MULTI);
/* 346 */     replaceMulti(RFC2252_BINARY_MULTI);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testRemove() throws Exception {
/* 351 */     remove(ZIMBRA_BINARY_MULTI, false);
/* 352 */     remove(ZIMBRA_BINARY_MULTI, true);
/*     */     
/* 354 */     remove(CERTIFICATE_MULTI, false);
/* 355 */     remove(CERTIFICATE_MULTI, true);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   @Test
/*     */   public void testAdd()
/*     */     throws Exception
/*     */   {
/* 364 */     add(ZIMBRA_BINARY_MULTI, false);
/* 365 */     add(ZIMBRA_BINARY_MULTI, true);
/*     */     
/* 367 */     add(CERTIFICATE_MULTI, false);
/* 368 */     add(CERTIFICATE_MULTI, true);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/ldap/TestLdapBinary.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */